package com.software.shipperagent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.form.billform.dao.BillForm;
import com.software.form.billform.dao.BillFormTransaction;
import com.software.form.bookingform.dao.BookingFormTransaction;
import com.software.form.cargobooking.dao.CargoBooking;
import com.software.form.cargobooking.dao.CargoBookingTransaction;
import com.software.form.inquiry.sagent.dao.InquirySagentTransaction;
import com.software.form.inquiryform.dao.InquiryForm;
import com.software.form.inquiryform.dao.InquiryFormTransaction;
import com.software.form.interchangeform.dao.InterchangeForm;
import com.software.form.interchangeform.dao.InterchangeFormTransaction;
import com.software.form.packinglist.dao.PackingListTransaction;
import com.software.form.shippingorder.dao.ShippingOrderTransaction;

public class ShippmentServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		InterchangeFormTransaction ift = (InterchangeFormTransaction)CommonUtils.getBean("interchangeFormTransaction");
		String email = (String) request.getSession().getAttribute("user");
		List<InterchangeForm> inters = ift.getInterFormsByEmail(email);
		List list = new ArrayList();
		if(inters != null){
			for(int i = 0; i < inters.size(); i++){
				JSONArray array = new JSONArray();
				array.put(inters.get(i).getNo());
				array.put(inters.get(i).getCompany());
				array.put(inters.get(i).getUser());
				array.put(inters.get(i).getDeliveryPlace());
				array.put(inters.get(i).getDeliverTo());
				array.put(inters.get(i).getReturnPlace());
				array.put(inters.get(i).getTimeout());
				list.add(array);
			}
			JSONObject o = new JSONObject();
			try {
				o.put("inters", list);
				CommonUtils.outputJsonData(o.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		JSONObject json = CommonUtils.readJSONStringFromRequestBody(request, response);
		PackingListTransaction plt = (PackingListTransaction)CommonUtils.getBean("packingListTransaction");
		InterchangeFormTransaction ift = (InterchangeFormTransaction)CommonUtils.getBean("interchangeFormTransaction");
		ShippingOrderTransaction sot = (ShippingOrderTransaction)CommonUtils.getBean("shippingOrderTransaction");
		String email = (String) request.getSession().getAttribute("user");
		try{
			String inter_no = json.getString("inter_no");
			String motoEmail = json.getString("motoEmail");
			String no = json.getString("no");
			String shipper = json.getString("shipper");
			String cno = json.getString("cno");
			String ov = json.getString("ov");
			String cargo = json.getString("cargo");
			String boxes = json.getString("boxes");
			String quantity = json.getString("quantity");
			String up = json.getString("up");
			String tp = json.getString("tp");
			String uv = json.getString("uv");
			String tv = json.getString("tv");
			String tw = json.getString("tw");
			String poo = json.getString("poo");
			
			if(plt.add(no, shipper, email, cno, ov, cargo, boxes, quantity, up, tp, uv, tv, tw, poo, CommonUtils.getNowTime())){
				if(ift.updateMotoEmail(inter_no, email, motoEmail)){
					if(sot.add(no, inter_no, motoEmail, 0))
						CommonUtils.outputJsonData(1, response);
				}
			}
			else
				CommonUtils.outputJsonData(0, response);
		}catch(JSONException je){
			je.printStackTrace();
			try {
				CommonUtils.outputJsonData(0, response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	
}
